Java POI 读取Excel - 魔霄 - 博客频道 - CSDN.NET

创建时间:2016/11/4 11:21
来源:http://blog.csdn.net/muxuexijian/article/details/8654452


2013-03-09 19:37 5207人阅读 评论(2) 收藏 举报
.
分类:
java(3)
.

版权声明:本文为博主原创文章,未经博主允许不得转载。

Apache POI :http://poi.apache.org/

http://poi.apache.org/download.html

解析xls格式的包:
poi-3.9-20121203.jar

解析xlsx格式的包:
poi-3.9-20121203.jar
poi-ooxml-3.9-20121203.jar
xmlbeans-2.3.0.jar
poi-ooxml-schemas-3.9-20121203.jar
dom4j-1.6.1.jar


  1. package cjw;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.IOException;  
  7. import java.io.InputStream;  
  8.   
  9. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  10. import org.apache.poi.ss.usermodel.Cell;  
  11. import org.apache.poi.ss.usermodel.Row;  
  12. import org.apache.poi.ss.usermodel.Sheet;  
  13. import org.apache.poi.ss.usermodel.Workbook;  
  14. import org.apache.poi.xssf.usermodel.XSSFWorkbook;  
  15.   
  16. public class ReadFile {  
  17.   
  18.     /** 
  19.      * @param args 
  20.      */  
  21.     public static void main(String[] args) {  
  22.         // TODO 自动生成方法存根  
  23.         File file = new File("G:/aaa/01.xls");  
  24.         if(!file.exists()){  
  25.             System.out.println("文件不存在");  
  26.             return;  
  27.         }  
  28.         ReadFile rf = new ReadFile();  
  29.         rf.readExcel(file);  
  30.     }  
  31.       
  32.     /** 
  33.      * 读取Excel数据 
  34.      * @param file 
  35.      */  
  36.     public void readExcel(File file){  
  37.         try {  
  38.             InputStream inputStream = new FileInputStream(file);  
  39.             String fileName = file.getName();  
  40.             Workbook wb = null;  
  41.             if(fileName.endsWith("xls")){  
  42.                 wb = new HSSFWorkbook(inputStream);//解析xls格式  
  43.             }else if(fileName.endsWith("xlsx")){  
  44.                 wb = new XSSFWorkbook(inputStream);//解析xlsx格式  
  45.             }  
  46.             Sheet sheet = wb.getSheetAt(0);//第一个工作表  
  47.               
  48.             int firstRowIndex = sheet.getFirstRowNum();  
  49.             int lastRowIndex = sheet.getLastRowNum();  
  50.             for(int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex ++){  
  51.                 Row row = sheet.getRow(rIndex);  
  52.                 if(row != null){  
  53.                     int firstCellIndex = row.getFirstCellNum();  
  54.                     int lastCellIndex = row.getLastCellNum();  
  55.                     for(int cIndex = firstCellIndex; cIndex < lastCellIndex; cIndex ++){  
  56.                         Cell cell = row.getCell(cIndex);  
  57.                         String value = "";  
  58.                         if(cell != null){  
  59.                             value = cell.toString();  
  60.                             System.out.print(value+"\t");  
  61.                         }  
  62.                     }  
  63.                     System.out.println();  
  64.                 }  
  65.             }  
  66.         } catch (FileNotFoundException e) {  
  67.             // TODO 自动生成 catch 块  
  68.             e.printStackTrace();  
  69.         } catch (IOException e) {  
  70.             // TODO 自动生成 catch 块  
  71.             e.printStackTrace();  
  72.         }  
  73.     }  
  74. }  


.
1
0
.

我的同类文章

java(3)